package com.dswz.utils;

import java.io.PrintStream;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncryptUtil {
	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n += 256;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String encodeByMD5(String originalString) throws Exception {
		if (originalString != null) {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] results = md.digest(originalString.getBytes("UTF-8"));
			String resultString = byteArrayToHexString(results);
			return resultString.toUpperCase();
		}
		return null;
	}

	private static String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		// 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			// 把负数转换为正数
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			// 小于0F的数需要在前面补0
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	public static byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;
		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i += 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[(i / 2)] = ((byte) Integer.parseInt(strTmp, 16));
		}
		return arrOut;
	}

	public static SecretKey getADesKey() throws Exception {
		KeyGenerator kg = KeyGenerator.getInstance("DES");
		kg.init(56);
		SecretKey key = kg.generateKey();
		return key;
	}

	public static Key getADesKey(byte[] bytes) throws Exception {
		byte[] arrB = new byte[8];
		for (int i = 0; (i < bytes.length) && (i < arrB.length); i++) {
			arrB[i] = bytes[i];
		}
		Key key = new SecretKeySpec(arrB, "DES");
		return key;
	}

	public static Key getADesKey(String keyStr) throws Exception {
		byte[] bytes = keyStr.getBytes();
		return getADesKey(bytes);
	}

	public static Key getDefaultKey() throws Exception {
		return getADesKey(defaultKey);
	}

	public static byte[] encrypt(Key key, byte[] bytes) throws Exception {
		Cipher cp = Cipher.getInstance("DES");
		cp.init(1, key);
		return cp.doFinal(bytes);
	}

	public static String encrypt(Key key, String strIn) throws Exception {
		return byteArr2HexStr(encrypt(key, strIn.getBytes()));
	}

	public static byte[] decrypt(Key key, byte[] bytes) throws Exception {
		Cipher cp = Cipher.getInstance("DES");
		cp.init(2, key);
		return cp.doFinal(bytes);
	}

	public static String decrypt(Key key, String strIn) throws Exception {
		return new String(decrypt(key, hexStr2ByteArr(strIn)));
	}

	private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
			"e", "f" };
	private static String defaultKey = "2018dswz0615";

	public static void main(String[] args) throws Exception {
		System.out.println(encrypt(getDefaultKey(), "123456"));
		System.out.println(decrypt(getDefaultKey(), "33547239f0e66b2b"));
	}
}
